//
//  Network.m
//  rss_reader
//
//  Created by Oleg Soldatoff on 4/10/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "Network.h"
#import "XMLParser.h"


@implementation Network
@synthesize receivedData = _receivedData;
@synthesize feedDic = _feedDic;

static Network *sharedNetwork = nil;

+ (Network *)sharedNetwork {
  
  if (sharedNetwork == nil) {
    sharedNetwork = [[Network alloc] init];
  }
  return sharedNetwork;
  
}

- (id)init {
  
  self = [super init];
  if (self) {
      
  }
  return self;
}


- (void)dealloc {
  
  [_receivedData release];
  _receivedData = nil;
  [_feedDic release];
  _feedDic = nil;
  [super dealloc];
  
}

- (void) giveXML: (NSDictionary *) feedDic {
  
  self.feedDic = feedDic;
  
  NSURLRequest *theRequest=[NSURLRequest requestWithURL:
                            [NSURL URLWithString:[self.feedDic valueForKey:@"URL"]]
                                            cachePolicy:NSURLRequestReloadIgnoringCacheData
                                        timeoutInterval:60.0];
  
  // create the connection with the request
  // and start loading the data
  
  NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
  
  if (theConnection) {
    
    // Create the NSMutableData to hold the received data.
    // receivedData is an instance variable declared elsewhere.
    self.receivedData = [NSMutableData data];
    
  } else {
    
    // Inform the user that the connection failed.
    NSLog(@"Connection failed");
    
  }
  
}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  
  // This method is called when the server has determined that it
  // has enough information to create the NSURLResponse.
  // It can be called multiple times, for example in the case of a
  // redirect, so each time we reset the data.
  // receivedData is an instance variable declared elsewhere.
  [self.receivedData setLength:0];
  
}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  
  // Append the new data to receivedData.
  // receivedData is an instance variable declared elsewhere.
  [self.receivedData appendData:data];
  
}


- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  
  // release the connection, and the data object
  [connection release];
  
  // receivedData is declared as a method instance elsewhere
  self.receivedData = nil;
  
  // inform the user
  NSLog(@"Connection failed! Error - %@ %@",
        [error localizedDescription],
        [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
  [[NSNotificationCenter defaultCenter] postNotificationName:@"ErrorInNetwork" object:self];

  
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{
  
  // do something with the data
  // receivedData is declared as a method instance elsewhere
  
  NSLog(@"Succeeded! Received %d bytes of data",[self.receivedData length]);
  
//  NSString *xmlCheck = [[[NSString    alloc] initWithData:self.receivedData
//                                                 encoding:NSUTF8StringEncoding] autorelease];
//  NSLog(@"FROM Network = %@", xmlCheck);
  //NSDictionary *urlInfo = [NSDictionary dictionaryWithObject:self.url forKey:@"URL"];
   
  [[NSNotificationCenter defaultCenter] postNotificationName:@"xmlRecieved" object:self userInfo:self.feedDic];
  
  
   // release the connection, and the data object
  [connection release];
  //self.receivedData = nil;
  
}


- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
  return nil;
}


@end
